﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;

namespace Brain
{
    public class DefaultTimeManager : ITimeManager
    {
        public void DeserializedAll()
        {
        }
        public Dictionary<string, Stopwatch> Stopwatches;

        public void StartMeasuring(string key)
        {
            if (!Stopwatches.ContainsKey(key))
            {
                Stopwatch watch = new Stopwatch();
                Stopwatches[key] = watch;
            }

            Stopwatch w = Stopwatches[key];
            w.Reset();
            w.Start();
        }

        public float EndMeasuring(string key)
        {
            if (Stopwatches.ContainsKey(key))
            {
                Stopwatch w = Stopwatches[key];
                w.Stop();

                return (float)w.Elapsed.TotalMilliseconds;
            }
            return 0;
        }

        public float GetMeasuring(string key)
        {
            if (Stopwatches.ContainsKey(key))
            {
                Stopwatch w = Stopwatches[key];
                return (float)w.Elapsed.TotalMilliseconds;
            }
            return 0;
        }

        private float TotalTime;
        private float Time;
        private bool started;
        public GameTime MakeGameTimeUpdate()
        {
            if (started)
            {
                Time = EndMeasuring("_gameTime");
                TotalTime += Time;
                started = false;
            }
            else
            {
                StartMeasuring("_gameTime");
                started = true;
            }

            return new GameTime(TimeSpan.FromMilliseconds(TotalTime), TimeSpan.FromMilliseconds(Time));
        }

        private float d_TotalTime;
        private float d_Time;
        private bool d_started;
        public GameTime MakeGameTimeDraw()
        {
            if (d_started)
            {
                d_Time = EndMeasuring("_d_gameTime");
                d_TotalTime += Time;
                d_started = false;
            }
            else
            {
                StartMeasuring("_d_gameTime");
                d_started = true;
            }

            return new GameTime(TimeSpan.FromMilliseconds(d_TotalTime), TimeSpan.FromMilliseconds(d_Time));
        }

        public void Reset()
        {
            this.Stopwatches.Clear();
            this.Stopwatches = new Dictionary<string, Stopwatch>();
        }

        public DefaultTimeManager()
        {
            this.Stopwatches = new Dictionary<string, Stopwatch>();
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }


    }
}
